/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2023-02-12
 *
 * 数据表单钩子
 */
import { useBase } from "@/hooks/base/useBase"
import { useStore } from "@/hooks/store/useStore"

export const useForm = (model, dataForm) => {

  const { router, query, i18n } = useBase()
  const { base } = useStore()

  const formRef = ref()

  // 表单提交
  const dataFormSubmit = (form) => {
    form.validate(async (valid, fields) => {
      if (valid) {
        $http({
          url: $http.adornUrl('/' + model + '/handle'),
          method: 'post',
          data: $http.adornData(dataForm),
        }).then(({ data }) => {
          if (data && data.status === 200) {
            $message.success(i18n('common.handle_success'))
            router.go(-1)
          } else {
            $message.error(data.message)
          }
        })
      }
    })
  }

  const resetForm = (form) => {
    form.resetFields()
  }

  return {
    formRef,
    dataFormSubmit,
    resetForm,
  }
}
